package com.guragura.widget;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.guragura.Guragura;
import com.guragura.screens.GameScreen;
import com.guragura.screens.MainMenuScreen;
import com.guragura.util.Constants;

public class ExitDialog extends Dialog {

	private Image rootLayer;
	private Image background;
	private Image background2;
	private Button yesButton;
	private Button noButton;
	private Guragura guragura;
	private boolean isMainMenu;

	public ExitDialog(float width, float height, Guragura guragura,
			boolean mainMenu) {
		super(width, height);
		this.isMainMenu = mainMenu;
		this.guragura = guragura;
		// TODO Auto-generated constructor stub
		yesButton = new Button(guragura.atlasInterface.findRegion(
				Constants.YES_BUTTON, 1), guragura.atlasInterface.findRegion(
				Constants.YES_BUTTON, 2));
		noButton = new Button(guragura.atlasInterface.findRegion(
				Constants.NO_BUTTON, 1), guragura.atlasInterface.findRegion(
				Constants.NO_BUTTON, 2));
		background = new Image(
				guragura.atlasInterface.findRegion(Constants.ARE_YOU_SURE_TEXT));
		rootLayer = new Image(
				guragura.atlasInterface.findRegion(Constants.PAUSE_SCREEN));

		if (isMainMenu) {
			background2 = new Image(
					guragura.atlasInterface
							.findRegion(Constants.ARE_YOU_SURE_SCREEN));
		}

		yesButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				if (getGura().gameState == Guragura.MAIN_MENU) {
					getGura().exitGame();
				} else if (getGura().gameState == Guragura.GAME) {
					((GameScreen) getGura().gameScreen).exitScreen();
				}
			}
		});

		noButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				if (getGura().gameState == Guragura.MAIN_MENU) {
					parent.removeActor(getSelf());
					((MainMenuScreen) getGura().mainMenuScreen).isExitDialog = false;
				} else if (getGura().gameState == Guragura.GAME) {
					parent.removeActor(getSelf());
					((GameScreen) getGura().gameScreen).isExitDialog = false;
					((GameScreen) getGura().gameScreen).isPauseDialog = true;
					((GameScreen) getGura().gameScreen).stage
							.addActor(((GameScreen) getGura().gameScreen).pauseDialog);

				}

			}
		});

		yesButton.x = (320 - yesButton.width) / 2;
		yesButton.y = (480 - yesButton.height) / 2;

		noButton.x = (320 - noButton.width) / 2 + 10;
		noButton.y = yesButton.y - yesButton.height;

		if (isMainMenu) {
			addActor(background2);
		}

		addActor(rootLayer);
		addActor(background);
		addActor(yesButton);
		addActor(noButton);
	}

	public Guragura getGura() {
		return guragura;
	}

	public ExitDialog getSelf() {
		return this;
	}

}
